package com.collectplus.express.parcel;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppLogic;
import com.collectplus.express.logic.DialogMgr;
import com.collectplus.express.model.Const;
import com.collectplus.express.model.ParcelBillBean;
import com.collectplus.express.tools.BarCodeUtil;
import com.collectplus.express.tools.DensityUtils;

import droid.frame.utils.lang.Str;

/**
 * @author kaka<br>
 *         2017-1-9下午12:00:28
 */
public class ParcelDigitalBillActivity extends BaseActivity2 implements View.OnClickListener {
    private ParcelBillBean billBean;
    private int parcelStatus;
    private String parcelId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        parcelStatus = getIntent().getIntExtra("parcelStatus", 0);
        parcelId = getIntent().getStringExtra("parcelId");
        super.onCreate(savedInstanceState);
        // showLoadingBar();
        // 代收..
        billBean = (ParcelBillBean) getIntent().getSerializableExtra("parcelBill");
        setViewValue(billBean);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.parcel_digital_bill);
        super.findViewById();
        getAppTitle().setCommonTitle("电子面单");
        findViewById(R.id.parcel_shared_bt).setOnClickListener(this);
        if (parcelStatus == 0) {
            findViewById(R.id.parcel_bill).setVisibility(View.GONE);
        }

    }

    private void setViewValue(ParcelBillBean billBean) {
        ImageView bigExpressLogo = (ImageView) findViewById(R.id.express_logo_big);
        AppLogic.getInstance().loadImage(billBean.getExpressLogo(), bigExpressLogo);

        TextView receiverArea = (TextView) findViewById(R.id.parcel_area);
        receiverArea.setText(billBean.getReceiverCity() + " " + billBean.getReceiverDistrict());
        // 收件联
        TextView expressCode = (TextView) findViewById(R.id.parcel_express_code);
        TextView signReceiver = (TextView) findViewById(R.id.prarcel_bill_receiver);
        TextView signReceiverPhone = (TextView) findViewById(R.id.prarcel_receiver_phone);
        TextView signReceiverAddress = (TextView) findViewById(R.id.prarcel_receiver_address);
        TextView signSender = (TextView) findViewById(R.id.prarcel_bill_sender);
        TextView signSenderPhone = (TextView) findViewById(R.id.prarcel_sender_phone);
        TextView signSenderAddress = (TextView) findViewById(R.id.prarcel_sender_address);

        expressCode.setText(billBean.getExpressNumber());
        signReceiver.setText(billBean.getReceiverName());
        signReceiverPhone.setText(billBean.getReceiverPhone());
        signReceiverAddress.setText(billBean.getReceiverAddress());
        signSender.setText(billBean.getSenderName());
        signSenderPhone.setText(billBean.getSenderPhone());
        signSenderAddress.setText(billBean.getSenderAddress());
        ImageView smallExpressLogo = (ImageView) findViewById(R.id.express_logo_small);
        AppLogic.getInstance().loadImage(billBean.getExpressLogo(), smallExpressLogo);
        // 收件联
        TextView parcelCode = (TextView) findViewById(R.id.receiver_page_parcel_code);
        TextView receiveReceivername = (TextView) findViewById(R.id.bill_receive_page_receiver);
        TextView receiveReceicerPhone = (TextView) findViewById(R.id.bill_receiver_page_receive_phone);
        TextView receiveReceicerAddress = (TextView) findViewById(R.id.bill_receiver_page_receive_address);
        TextView receiveSenderName = (TextView) findViewById(R.id.bill_receiver_page_sender);
        TextView receiveSenderPhone = (TextView) findViewById(R.id.bill_receiver_page_sender_phone);
        TextView receiveSenderAddress = (TextView) findViewById(R.id.bill_receiver_page_sender_address);
        TextView parcelCategory = (TextView) findViewById(R.id.parcel_category);

        // 二维码生成设置
        if (!Str.isEmpty(billBean.getExpressNumber())) {
            try {
                Bitmap largeCode = BarCodeUtil.createOneDCode(billBean.getExpressNumber(), DensityUtils.dip2px(this, 208), DensityUtils.dip2px(this, 52));
                Bitmap smallCode = BarCodeUtil.createOneDCode(billBean.getExpressNumber(), DensityUtils.dip2px(this, 84), DensityUtils.dip2px(this, 20));
                ImageView barCode = (ImageView) findViewById(R.id.erwei_code);
                ImageView smallBarCode = (ImageView) findViewById(R.id.receiver_page_code_img);
                if (largeCode != null) {
                    barCode.setImageBitmap(largeCode);
                } else {
                    barCode.setVisibility(View.GONE);
                }
                if (smallCode != null) {
                    smallBarCode.setImageBitmap(smallCode);
                } else {
                    smallBarCode.setVisibility(View.GONE);
                }
                parcelCode.setText(billBean.getExpressNumber());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 寄件联
        receiveReceivername.setText(billBean.getReceiverName());
        receiveReceicerPhone.setText(billBean.getReceiverPhone());
        receiveReceicerAddress.setText(billBean.getReceiverAddress());
        receiveSenderName.setText(billBean.getSenderName());
        receiveSenderPhone.setText(billBean.getSenderPhone());
        receiveSenderAddress.setText(billBean.getSenderAddress());
        parcelCategory.setText("邮寄物品: " + billBean.getCategory());

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.parcel_shared_bt:
                if (Str.isEmpty(parcelId)) {
                    showToast("分享失败!");
                    return;
                }
                if (parcelStatus == Const.ParcelStatus.payed) {
                    showToast("完成投递后才可以分享包裹路由信息给收件人哦！");
                } else {
                    DialogMgr.parcelRouteShare(this, parcelId);
                }
                break;
            default:
                break;
        }
    }
}
